package cn.wolfcode.controller;

import cn.wolfcode.domain.Banner;
import cn.wolfcode.domain.Strategy;
import cn.wolfcode.domain.Travel;
import cn.wolfcode.query.BannerQuery;
import cn.wolfcode.repository.BannerRepository;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IBannerService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.service.ITravelService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("banner")
public class BannerController {
    @Autowired
    private IBannerService bannerService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @RequestMapping("list")
    public Object list(Model model,BannerQuery qo){
        Page query = bannerService.query(qo);
        model.addAttribute("qo",qo);
        model.addAttribute("page",query);
        return "/banner/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner){
        if (!(banner.getId() == null)){
            bannerService.update(banner);
        }else {
            bannerService.insert(banner);
        }
        return JsonResult.success();
    }

    @RequestMapping("listDest")
    @ResponseBody
    public Object listDest(){
        return destinationService.listAll();
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(String id){
        if (id != null){
            bannerService.delete(id);
            return JsonResult.success();
        }
        return JsonResult.defaultError();
    }
    @RequestMapping("getAllType")
    @ResponseBody
    public Object getAllType(String id){
        List<Travel> ts = travelService.listAll();
        List<Strategy> sts = strategyService.listAll();
        Map<String, Object> map = new HashMap<>();
        map.put("sts",sts);
        map.put("ts",ts);
        return JsonResult.success(map);
    }



}
